package com.example.account;

import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

public class Jianpan {
    private KeyboardView keyboardView;
    private EditText editText;
    private Keyboard mykey; // 自定义键盘
    private boolean hasInput = false; // 是否已经输入了数字
    private boolean hasOperator = false; // 是否已经输入了操作符
    private String operator = ""; // 当前的操作符
    private double firstNumber = 0; // 第一个输入的数字
    private double secondNumber = 0; // 第二个输入的数字
    private boolean hasDecimalPoint = false; // 是否已经输入了小数点
    private boolean waitingForNumber = false; // 是否等待输入数字

    public interface OnEnsureListener {
        public void onEnsure();
    }

    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public Jianpan(KeyboardView keyboardView, EditText editText) {
        this.keyboardView = keyboardView;
        this.editText = editText;
        this.editText.setInputType(InputType.TYPE_NULL); // 不弹出系统键盘
        mykey = new Keyboard(this.editText.getContext(), R.xml.jianpan);

        this.keyboardView.setKeyboard(mykey); // 设置要显示键盘的样式
        this.keyboardView.setEnabled(true); // 可以进行点击事件
        this.keyboardView.setPreviewEnabled(false); // 禁止弹出
        this.keyboardView.setOnKeyboardActionListener(listener);
    }

    KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int i) {

        }

        @Override
        public void onRelease(int i) {

        }

        @Override
        public void onKey(int i, int[] ints) {
            Editable editable = editText.getText();
            int start = editText.getSelectionStart();
            if (i == -1) { // 删除
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            } else if (i == -2) { // 确定/等于
                if (hasInput && hasOperator) {
                    calculateAndDisplayResult();
                } else if (hasInput) {
                    onEnsureListener.onEnsure();
                    System.out.println(editText.getText().toString());
                }
            }else if (i == -3) { // 加法
                handleOperatorInput("+");
            } else if (i == -4) { // 减法
                handleOperatorInput("-");
            } else if (i == 46) { // 小数点
                if (!hasDecimalPoint) {
                    editable.insert(start, ".");
                    hasDecimalPoint = true;
                }
            } else {
                editable.insert(start, Character.toString((char) i));
                hasInput = true;
                waitingForNumber = false;
            }
        }

        @Override
        public void onText(CharSequence charSequence) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };

    private void handleOperatorInput(String newOperator) {
        Editable editable = editText.getText();
        if (hasInput) {
            if (!hasOperator) {
                firstNumber = Double.parseDouble(editable.toString());
                editable.append(newOperator);
                hasOperator = true;
                operator = newOperator;
                hasDecimalPoint = false; // 重置小数点状态
                waitingForNumber = true; // 等待输入数字
            } else if (!waitingForNumber) {
                calculateAndDisplayResult();
                editable.append(newOperator);
                hasOperator = true;
                operator = newOperator;
                hasDecimalPoint = false; // 重置小数点状态
                waitingForNumber = true; // 等待输入数字
            }
        }
    }

    private void calculateAndDisplayResult() {
        Editable editable = editText.getText();
        String[] parts = editable.toString().split("[+-]");
        if (parts.length == 2) {
            secondNumber = Double.parseDouble(parts[1]);
            double result = operator.equals("+") ? firstNumber + secondNumber : firstNumber - secondNumber;
            editable.clear();
            editable.append(String.valueOf(result));
            firstNumber = result;
            hasOperator = false;
            operator = "";
            hasDecimalPoint = String.valueOf(result).contains("."); // 更新小数点状态
            waitingForNumber = false; // 不再等待输入数字
        }
    }

    public void show() {
        // 显示键盘
        int now = keyboardView.getVisibility();
        if (now == View.INVISIBLE || now == View.GONE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    public void hide() {
        // 隐藏键盘
        int now = keyboardView.getVisibility();
        if (now == View.VISIBLE || now == View.INVISIBLE) {
            keyboardView.setVisibility(View.GONE);
        }
    }
}